// -*-Igor-*-
// ###################################################################
//  Igor Pro - JEG Tools
// 
//  FILE: "JEG Batch Loader"
//                                    created: 8/14/1998 {10:50:14 AM} 
//                                last update: 8/14/1998 {10:57:24 AM} 
//  Author: Jonathan Guyer
//  E-mail: <jguyer@his.com>
//     www: http://www.his.com/~jguyer/
//  
//  Description: 
// 
//  History
// 
//   modified  by  rev reason
//  ---------- --- --- -----------
//  8/14/1998  JEG 1.0 original
// ###################################################################
// 

#pragma rtGlobals=1		// Use modern global access method.

#include <File Name Utilities>
#include <Strings as Lists>
#include <NumInList>
#include <Concatenate Waves>

#include "JEG File Name Utilities"


// 
// -------------------------------------------------------------------------
// 
// "JEG_BatchLoadFiles" --
// 
//  Takes a list of ";" separated waveNames and prompts for the first
//  file in a batch of similarly named files (assumes they're named something 
//  like 'base123.ext'). Loads data from all files with that file number 
//  (e.g., 123) or greater and concatenates their results into the waves as 
//  given in waveNames. If more columns are loaded than have supplied names,
//  their results are lost.
// -------------------------------------------------------------------------
// 
Function JEG_BatchLoadFiles(waveNames)
	String waveNames
	
	String fileType="????"
	
	Variable batchFile
	Open/D/R/T=fileType/M="Select the first data file in the batch:" batchFile
	
	if (strlen(S_fileName))		// else user cancelled
	
		// break down the file name into its components
		// assumes it's named something like 'base123.ext'
		String path = FilePathOnly(S_fileName)
		String file = FileNameOnly(S_fileName)
		String ext = FileExtension(S_fileName)
		
		String base = JEG_BaseAndNumber(file)
		Variable num = str2num(GetStrFromList(base, 1, ";"))
		base = GetStrFromList(base, 0, ";")
				
		// Just to get things set up		
		
		String loadFile
		Variable tempCount, waveCount = 0
		do
			sprintf loadFile, "%s%s%u%s", path, base, num, ext
			tempCount = JEG_LoadAndAppend(waveNames, loadFile)
			if	(tempCount == 0) // file didn't exist
				break
			else
				// on the off-chance that not all files have the same number of columns
				waveCount = Max(waveCount,tempCount)
			endif
			num += 1
		while (1)
		
		Variable i = 0
		do
			KillWaves $("JEG_batch" + num2str(i))
			i += 1
		while (i < waveCount)
		
		return 1	// batch load successful
	else
		return 0 // batch load cancelled
	endif
End


Function JEG_LoadAndAppend(waveNames, fileName)
	String waveNames, fileName
	
	// check that file exists
	Variable junk
	Open/Z/R/T="????" junk as fileName
	
	Variable result = 0
	if (V_flag == 0)
		Close junk
		LoadWave/O/Q/G/N=JEG_batch fileName
		
		result = V_flag	// load was OK
		
		Variable count = NumInList(waveNames, ";")
		Variable i = 0
		String concatenee, concatener
		do
			concatenee = GetStrFromList(waveNames, i, ";")
			concatener = "JEG_batch" + num2str(i)
			ConcatenateWaves(concatenee, concatener)
			i += 1
		while (i < count)
	endif
	
	return result
End


